package com.yiyibook.controller;

import com.yiyibook.entity.BookBigStyle;
import com.yiyibook.entity.BookSmallStyle;
import com.yiyibook.mapper.BookBigStyleMapper;
import com.yiyibook.service.BookBigStyleService;
import com.yiyibook.service.BookSmallStyleService;
import com.yiyibook.util.FieldErrorMsg;
import com.yiyibook.util.Msg;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 图书大类 Controller
 *
 * @author yi.wu
 * @date 2019-10-14
 */
@RestController
@RequestMapping("/big")
public class BookBigStyleController {

    @Autowired
    private BookBigStyleService bookBigStyleService;

    @Autowired
    private BookBigStyleMapper bookBigStyleMapper;

    @Autowired
    private BookSmallStyleService bookSmallStyleService;

    @RequestMapping("/selectAll")
    public Msg selectAll(){
        List<BookBigStyle> bookBigStyleList = bookBigStyleMapper.selectAllByCondition(null);
        return Msg.success().add("bookBigStyleList", bookBigStyleList);
    }

    @PostMapping("/insert")
    public Msg insertBigStyle(@Valid BookBigStyle bookBigStyle, BindingResult result){
        if (result.hasErrors()) {
            //校验失败，显示校验失败的错误信息
            return Msg.fail().add("errorMsg", FieldErrorMsg.errorMsg(result));
        }else{
            BookBigStyle bookBigStyle1 = bookBigStyleService.selectOneByCondition(bookBigStyle);
            if(bookBigStyle1 != null){
                return Msg.fail().add("errorMsg","图书大类名称已存在，请重新输入！");
            }else{
                int i = bookBigStyleService.insertSelective(bookBigStyle);
                if(i>0){
                    return Msg.success().add("successMsg","添加成功");
                }else{
                    return Msg.fail().add("errorMsg","网络波动，请重试");
                }
            }
        }
    }

    @GetMapping("/selectOne")
    public BookBigStyle selectOne(Integer bookBigStyleId){
        BookBigStyle bookBigStyle = new BookBigStyle();
        bookBigStyle.setBigStyleId(bookBigStyleId);
        return bookBigStyleService.selectOneByCondition(bookBigStyle);
    }

    @PostMapping("/update")
    public Msg update(@Valid BookBigStyle bookBigStyle, BindingResult result){
        Integer bigStyleId = bookBigStyle.getBigStyleId();
        if (result.hasErrors()) {
            //校验失败，显示校验失败的错误信息
            return Msg.fail().add("errorMsg", FieldErrorMsg.errorMsg(result));
        }else{
            bookBigStyle.setBigStyleId(null);
            BookBigStyle bookBigStyle1 = bookBigStyleService.selectOneByCondition(bookBigStyle);
            if(bookBigStyle1 != null){
                return Msg.fail().add("errorMsg","图书大类名称已存在，请重新输入！");
            }else{
                bookBigStyle.setBigStyleId(bigStyleId);
                int i = bookBigStyleService.updateByPrimaryKeySelective(bookBigStyle);
                if(i>0){
                    return Msg.success().add("successMsg","修改成功");
                }else{
                    return Msg.fail().add("errorMsg","网络波动，请重试");
                }
            }
        }
    }

    @GetMapping("/delete")
    public Msg delete(Integer bigStyleId){
        List<BookSmallStyle> smallStyleList = bookSmallStyleService.getAllByBigStyleId(bigStyleId);
        if(CollectionUtils.isNotEmpty(smallStyleList)){
            return Msg.fail().add("errorMsg","图书大类下还有小类，请先删除对应的图书小类！");
        }else{
            int i = bookBigStyleService.deleteByPrimaryKey(bigStyleId);
            if(i>0){
                return Msg.success().add("successMsg","删除成功");
            }else{
                return Msg.fail().add("errorMsg","网络波动，请重试");
            }
        }
    }

    @GetMapping("/deleteAny")
    public Msg deleteAny(String bigStyleIds){
        // 保存 无法被删除的 图书大类 （即图书大类下还存在小类的情况）
        String bigStyleName = "";
        // 记录 是否删除成功
        int j = 0;
        String[] split = bigStyleIds.split(", ");
        Integer[] bigStyleIdsArr = new Integer[split.length];
        for (int i = 0; i < bigStyleIdsArr.length; i++) {
            bigStyleIdsArr[i] = Integer.parseInt(split[i]);
        }
        for (int i = 0; i < bigStyleIdsArr.length; i++) {
            List<BookSmallStyle> smallStyleList = bookSmallStyleService.getAllByBigStyleId(bigStyleIdsArr[i]);
            if(CollectionUtils.isNotEmpty(smallStyleList)){
                BookBigStyle bookBigStyle = new BookBigStyle();
                bookBigStyle.setBigStyleId(bigStyleIdsArr[i]);
                bookBigStyle = bookBigStyleService.selectOneByCondition(bookBigStyle);
                if(i == bigStyleIdsArr.length-1){
                    bigStyleName = bigStyleName + bookBigStyle.getBigStyleName();
                }else{
                    bigStyleName = bigStyleName + bookBigStyle.getBigStyleName() + ",";
                }
            }else{
                j = bookBigStyleService.deleteByPrimaryKey(bigStyleIdsArr[i]);
            }
        }
        if(j > 0){
            return Msg.success().add("successMsg","删除成功");
        }else if(j == 0){
            return Msg.fail().add("errorMsg","以下 " + bigStyleName +" 图书大类下，还有图书小类，无法删除，请先删除小类!");
        }else{
            return Msg.fail().add("errorMsg","网络波动，请重试");
        }
    }
}
